package com.ssyt.ec.exam.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.ec.common.AppServlet;
import com.ssyt.ec.common.PageModel;
import com.ssyt.ec.common.util.DBUtil;
import com.ssyt.ec.common.util.FastjsonUtil;
import com.ssyt.ec.exam.entity.ExamPaperEntity;
import com.ssyt.ec.exam.entity.ExamRecordsEntity;
import com.ssyt.ec.exam.service.ExamRecordsService;

@WebServlet("/exam/ExamRecordsServlet")
public class ExamRecordsServlet extends AppServlet {
	private static final long serialVersionUID = 1L;
	private ExamRecordsService examrecordsService = new ExamRecordsService();

	public ExamRecordsServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("listExamRecords".equals(command)) {
			listExamRecords(request, response);
		} else if ("listpapername".equals(command)) {
			listpapername(request, response);
		} else if ("search".equals(command)) {
			search(request, response);
		} else if ("deleteExamRecords".equals(command)) {
			deleteExamRecords(request, response);
		} else {
			throw new IllegalStateException("请求非法~");
		}
	}

	/**
	 * 删除考试记录
	 * 
	 * @param request
	 * @param response
	 */
	private void deleteExamRecords(HttpServletRequest request,
			HttpServletResponse response) {
		String ids = request.getParameter("ids");
		int row = DBUtil.executeUpdate("DELETE FROM exam-records WHERE id IN ("
				+ ids + ")");

		if (row >= 1) {
			print(response, 1);
		}
	}

	/**
	 * 查询所有考试记录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listExamRecords(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int page = Integer.parseInt(request.getParameter("page"));
		int rows = Integer.parseInt(request.getParameter("rows"));
		String sort = request.getParameter("sort");
		String order = request.getParameter("order");
		PageModel<ExamRecordsEntity> examrecords = examrecordsService
				.listExamRecords(sort, order, page, rows);
		print(response, FastjsonUtil.toJson(examrecords));
	}

	private void listpapername(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		List<ExamPaperEntity> paper = examrecordsService.listpapername();
		print(response, FastjsonUtil.toJson(paper));

	}

	/**
	 * 模糊查询
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void search(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String searchName = request.getParameter("searchName");

		String pageStr = request.getParameter("page");
		String rowStr = request.getParameter("rows");
		int page = 0;
		int rows = 0;
		if (pageStr != null && rowStr != null) {
			page = Integer.parseInt(pageStr);
			rows = Integer.parseInt(rowStr);
		}
		String papernameChoose = request.getParameter("papername_choose");
		PageModel<ExamRecordsEntity> examrecords = examrecordsService.search(
				page, rows, searchName, papernameChoose);
		print(response, FastjsonUtil.toJson(examrecords));
	}

}
